package com.dap.param;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by wangmin on 2019/6/4.
 */
public class ImportParam implements Serializable {
    private static final long serialVersionUID = -1;

    /**
     * Object[][] 或者 Object[][][]
     * */
    private Object data;
    private Map<String, String> extraParam;

    public ImportParam() {}
    public ImportParam(Object data, Map<String, String> extraParam) {
        this.data = data;
        this.extraParam = extraParam;
    }


    private static Object[][] convertJSONArrayToArray2(JSONArray jsonArray2) {
        Object[][] array2 = new Object[jsonArray2.size()][];

        for (int j=0; j<jsonArray2.size(); ++j) {
            JSONArray jsonArray1 = (JSONArray) jsonArray2.get(j);
            if (jsonArray1 == null) {
                continue;
            }

            Object[] array1 = array2[j] = new Object[jsonArray1.size()];
            for (int k=0; k<jsonArray1.size(); ++k) {
                array1[k] = jsonArray1.get(k);
            }
        }

        return array2;
    }
    private static Object convertData(Object data) {
        if (!(data instanceof JSONArray)) {
            return data;
        }

        JSONArray a0 = ((JSONArray)data);

        Object[][][] result3 = null;
        Object[][] result2 = null;
        if (!a0.isEmpty()) {
            Object a1 = a0.get(0);
            if (a1 instanceof JSONArray) {
                if (!((JSONArray)a1).isEmpty()) {
                    Object a2 = ((JSONArray)a1).get(0);
                    if (a2 instanceof JSONArray) {
                        result3 = new Object[a0.size()][][];
                    } else {
                        result2 = new Object[a0.size()][];
                    }
                } else {
                    result2 = new Object[a0.size()][];
                }
            }
        }

        if (result3 != null) {
            for (int i=0; i<a0.size(); ++i) {
                JSONArray a1 = (JSONArray) a0.get(i);
                if (a1 == null) {
                    continue;
                }
                result3[i] = convertJSONArrayToArray2(a1);
            }
            return result3;
        } else if (result2 != null) {
            return convertJSONArrayToArray2(a0);
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        Object ip;

        ip = JSON.parseObject("", Object.class);
        ip = convertData(ip);

        ip = JSON.parseObject("[[]]", Object.class);
        ip = convertData(ip);

        ip = JSON.parseObject("[[1]]", Object.class);
        ip = convertData(ip);

        ip = JSON.parseObject("[[[]]]", Object.class);
        ip = convertData(ip);

        ip = JSON.parseObject("[[[1,2,3]],[],[[],[4]]]", Object.class);
        ip = convertData(ip);
    }

    /**
     * 判断数据是否是三维数组, 3维数组一定是二维数组
     * */
    public boolean resolveIsDataThreeDimentional() {
        data = convertData(data);
        return data instanceof Object[][][];
    }


    public Object getData() {
        data = convertData(data);
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public Map<String, String> getExtraParam() {
        return extraParam;
    }
    public void setExtraParam(Map<String, String> extraParam) {
        this.extraParam = extraParam;
    }
}
